package com.runda.partypolicy.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;


import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;

import com.runda.partypolicy.app.di.component.DaggerAppComponent;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;


import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.multidex.MultiDex;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;
import dagger.android.support.HasSupportFragmentInjector;

/**
 * author:  RD_YH
 * date:    2018/10/9
 * version: v1.0
 * description:
 */
public class ApplicationMine extends Application implements
        HasActivityInjector, HasSupportFragmentInjector {
    private static ApplicationMine ourInstance;
    @Inject
    DispatchingAndroidInjector<Activity> activityInjector;
    @Inject
    DispatchingAndroidInjector<Fragment> fragmentInjector;
    private boolean isSigned = false;
    private RefWatcher refWatcher;

    public static ApplicationMine getInstance() {
        return ourInstance;
    }


    public void setIsUserSigned(@NonNull Boolean isSigned) {
        this.isSigned = isSigned;

    }
    public boolean isUserSigned() {
        return isSigned;
    }



    public RefWatcher getRefWatcher() {
        return refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ourInstance = this;
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
        refWatcher = LeakCanary.install(this);


        initComponents();
//        initImagePicker();

    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Glide.get(this).onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Glide.get(this).onTrimMemory(level);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    private void initComponents() {
        MultiDex.install(this);
        DaggerAppComponent.builder().application(this).build().inject(this);
//        EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
        Utils.init(this);
        initLogger();
        initGalleryFinal();
    }

    private void initLogger() {
       /* FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag("MEDICINE_ALLIANCE")
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return Constants.DEBUG;
            }
        });*/
    }

    private void initGalleryFinal() {
//        RxGalleryFinalApi.setImgSaveRxSDCard(Constants.PATH_IMAGES);
//        RxGalleryFinalApi.setImgSaveRxCropSDCard(Constants.PATH_IMAGESCROP);
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return activityInjector;
    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return fragmentInjector;
    }

    /**
     * 图片剪裁器 初始化
     */
//    private void initImagePicker() {
//        ImagePicker imagePicker = ImagePicker.getInstance();
//        //设置图片加载器
//        imagePicker.setImageLoader(new LocalGlideImageLoader());
//        imagePicker.setMultiMode(false);
//        //显示拍照按钮
//        imagePicker.setShowCamera(true);
//        //允许裁剪（单选才有效）
//        imagePicker.setCrop(false);
//        //是否按矩形区域保存
//        imagePicker.setSaveRectangle(true);
//        //选中数量限制
//        imagePicker.setSelectLimit(9);
//        //裁剪框的形状
//        imagePicker.setStyle(CropImageView.Style.RECTANGLE);
//        //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
//        imagePicker.setFocusWidth(ScreenUtil.getScreenWidth(this) - DensityUtil.dip2px(this, 20));
//        //裁剪框的高度。单位像素（圆形自动取宽高最小值）
//        imagePicker.setFocusHeight(ScreenUtil.getScreenWidth(this) * 3 / 4);
//        //保存文件的宽度。单位像素
//        imagePicker.setOutPutX(800);
//        //保存文件的高度。单位像素
//        imagePicker.setOutPutY(800);
//    }



}
